using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Threading;

namespace Web {
	public class NormalizeRoute : Route {
		public NormalizeRoute(string url, IRouteHandler routeHandler) : base(url, routeHandler) { }
		public NormalizeRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler) { }
		public NormalizeRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler) : base(url, defaults, constraints, routeHandler) { }
		public NormalizeRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) : base(url, defaults, constraints, dataTokens, routeHandler) { }
		
		public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
			VirtualPathData path = base.GetVirtualPath(requestContext, values);
			
			if (path != null) {
				path.VirtualPath = path.VirtualPath.ToLowerInvariant();
			}
			
			return path;
		}
	}
	
	
	public static class RouteCollectionExtensions {
		public static Route MapNormalizeRoute(this RouteCollection routes, string name, string url, object defaults) {
			return RouteCollectionExtensions.MapNormalizeRoute(routes, name, url, defaults, null);
		}
		
		public static Route MapNormalizeRoute(this RouteCollection routes, string name, string url, object defaults, object constraints) {
			if (routes == null) {
				throw new ArgumentNullException("routes", "Route collection not be null");
			}
			
			if (url == null) {
				throw new ArgumentNullException("url", "Url not be null");
			}
			
			NormalizeRoute route = new NormalizeRoute(url, new MvcRouteHandler()) {
				Defaults = new RouteValueDictionary(defaults),
				Constraints = new RouteValueDictionary(constraints),
				DataTokens = new RouteValueDictionary()
			};
			
			routes.Add(name, route);
			
			return route;
		}
	}
	

	public class RouteConfig {
		public static void RegisterRoutes(RouteCollection routes) {
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
			routes.IgnoreRoute("{resource}.ndb");

			routes.MapNormalizeRoute(
				name: "Install-Localized",
				url: "{language}/install/{action}/{id}",
				defaults: new { language = "en", controller = "Install", action = "Index", id = UrlParameter.Optional }
			);

			routes.MapNormalizeRoute(
				name: "Install",
				url: "install/{action}/{id}",
				defaults: new { controller = "Install", action = "Index", language = UrlParameter.Optional, id = UrlParameter.Optional }
			);

			routes.MapNormalizeRoute(
				name: "Default",
				url: "{language}/{controller}",
				defaults: new { language = "en", controller = "CMS", action = "Index" }
			);
		}
		
		
		public static void OnBeginExecuteCore(Controller controller)
		{
			string lang = "";
			if (controller.RouteData.Values["language"] != null &&
				!string.IsNullOrWhiteSpace(controller.RouteData.Values["language"].ToString()))
			{
				// set the culture from the route data (url)
				lang = controller.RouteData.Values["language"].ToString();
				Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
			} else if(controller.Session[Nebulosa.Core.Constants.SessionCulture] != null) {
				lang = controller.Session[Nebulosa.Core.Constants.SessionCulture].ToString();
				Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
			}
			
			HttpCookie _cookie = new HttpCookie("aaaaaaaa", lang);
            _cookie.Expires = DateTime.Now.AddYears(1);
            controller.HttpContext.Response.SetCookie(_cookie);
		}
	}
	
	
	
	
	public class RouteConfigExtensions
	{
		public static void OnBeginExecuteCore(Controller controller)
		{
			string lang = "1";
			if (controller.RouteData.Values["language"] != null &&
				!string.IsNullOrWhiteSpace(controller.RouteData.Values["language"].ToString()))
			{
				// set the culture from the route data (url)
				lang = controller.RouteData.Values["language"].ToString();
				Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
			} else if(controller.Session[Nebulosa.Core.Constants.SessionCulture] != null) {
				lang = controller.Session[Nebulosa.Core.Constants.SessionCulture].ToString();
				Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
			}
			
			HttpCookie _cookie = new HttpCookie("aaaaaaaa", lang);
            _cookie.Expires = DateTime.Now.AddYears(1);
            controller.HttpContext.Response.SetCookie(_cookie);
		}
	}
}